package com.yuwubao.uav.tool.controller;

import com.yuwubao.framework.bean.JsonResult;
import com.yuwubao.framework.bean.PageData;
import com.yuwubao.framework.loginFilter.LoginRequired;
import com.yuwubao.uav.tool.service.UserService;
import com.yuwubao.uav.tool.util.MD5Util;
import com.yuwubao.uav.tool.util.StringUtil;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.NotEmpty;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.Min;

/**
 *
 */
@RestController
@RequestMapping("/bk")
public class AddressListController {

    private static final Logger logger = LogManager.getLogger(AddressListController.class);

    @Resource
    private UserService userService;

    @LoginRequired
    @PostMapping("/importUserList")
    @ApiOperation(value = "导入通讯录")
    public JsonResult importUserList(
            @ApiParam(value = "文件名称", required = true) @RequestParam("file") MultipartFile file,
            HttpServletRequest request
    ) {
        JsonResult result = new JsonResult();
        try {
            result = userService.importUserList(file,request);

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }
    @LoginRequired
    @GetMapping("/listAddressListCompany")
    @ApiOperation(value = "获取通讯录单位列表")
    public JsonResult listAddressListCompany() {
        JsonResult result = new JsonResult();
        try {
            result.setResult( userService.listAddressListCompany());
            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }
    @LoginRequired
    @GetMapping("/listAddressListDepartment")
    @ApiOperation(value = "获取通讯录单位部门列表")
    public JsonResult listAddressListDepartment(@ApiParam(value = "通讯录单位ID", required = true) @RequestParam("cId") String cId) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("cId",cId);
            result.setResult(userService.listAddressListDepartment(pageData));
            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }

    @LoginRequired
    @GetMapping("/listAddressList")
    @ApiOperation(value = "获取通讯录列表")
    public JsonResult listAddressList(
            @ApiParam(value = "通讯录单位ID") @RequestParam(value = "cId",required = false) String cId,
            @ApiParam(value = "通讯录单位部门ID") @RequestParam(value = "dId",required = false) String dId
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("cId",cId);
            pageData.put("dId",dId);
            result.setResult(userService.listAddressList(pageData));
            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }

    @LoginRequired
    @PostMapping("/deleteAddressList")
    @ApiOperation(value = "删除通讯录")
    public JsonResult deleteAddressList(@ApiParam(value = "通讯录ID,多个用逗号分隔") @RequestParam(value = "ids") String ids) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();

            if (StringUtil.isNotNvlOrBlank(ids)) {
                String[] idArr = ids.split(",");
                if (idArr != null && idArr.length > 0) {
                    for(String id:idArr){
                        pageData.put("id",id);
                        userService.deleteAddressList(pageData);
                    }
                }
            }

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }

    @LoginRequired
    @PostMapping("/deleteAddressListCompany")
    @ApiOperation(value = "删除通讯录单位")
    public JsonResult deleteAddressListCompany(@ApiParam(value = "通讯录单位ID,多个用逗号分隔") @RequestParam(value = "ids") String ids) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();

            if (StringUtil.isNotNvlOrBlank(ids)) {
                String[] idArr = ids.split(",");
                if (idArr != null && idArr.length > 0) {
                    for(String id:idArr){
                        pageData.put("id",id);
                        userService.deleteAddressListCompany(pageData);
                    }
                }
            }
            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }

    @LoginRequired
    @PostMapping("/deleteAddressListDepartment")
    @ApiOperation(value = "删除通讯录单位部门")
    public JsonResult deleteAddressListDepartment(@ApiParam(value = "通讯录单位部门ID,多个用逗号分隔") @RequestParam(value = "ids") String ids) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();

            if (StringUtil.isNotNvlOrBlank(ids)) {
                String[] idArr = ids.split(",");
                if (idArr != null && idArr.length > 0) {
                    for(String id:idArr){
                        pageData.put("id",id);
                        userService.deleteAddressListDepartment(pageData);
                    }
                }
            }
            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }



    @LoginRequired
    @PostMapping("/sendMsg")
    @ApiOperation(value = "发送短信")
    public JsonResult sendMsg(
            @ApiParam(value = "电话号码，多个用逗号分隔",required = true) @RequestParam(value = "uTels") String uTels,
            @ApiParam(value = "短信内容",required = true) @Length(max = 100) @RequestParam(value = "msgContent") String msgContent
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("msgContent",msgContent);
            pageData.put("uTels",uTels);
            userService.sendMsg(pageData);
            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }






}
